package com.glodon.paas.account.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.glodon.paas.consts.StringConst;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/")
public class HomeController {
    public static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView root() throws IOException {
        return new ModelAndView(new RedirectView("info"));
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        return new ModelAndView(new RedirectView("info"));
    }
    
    @RequestMapping(value = "/contactus", method = RequestMethod.GET)
    public ModelAndView contact() {
        return new ModelAndView("contact");
    }

    @RequestMapping(value = "/agree", method = RequestMethod.GET)
    public ModelAndView agree() {
        return new ModelAndView("agree");
    }
    
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView register(@RequestParam(value = "service", required = false, defaultValue = "account") String service,
    							 @RequestParam(value = "device", required = false) String device,
                                 @RequestParam Map<String, String> param,
                                 HttpServletRequest request) {
        param.put("service", service);
        if(request.getSession().getAttribute("loginUrlQueryString") == null){
        	request.getSession().setAttribute("loginUrlQueryString", request.getQueryString());
        }
        if(StringConst.MOBILE.equalsIgnoreCase(device)){
        	return new ModelAndView("device", param);
        }
        else{
        	return new ModelAndView("register", param);
        }
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(@RequestParam(value = "service", required = false, defaultValue = "account") String service,
                              @RequestParam(value = "hybrid", required = false, defaultValue = "off") String hybrid,
                              @RequestParam(value = "oauth_token", required = false, defaultValue = "dummy") String token,
                              @RequestParam(value = "device", required = false) String device,
                              @RequestParam Map<String, String> param,
                              HttpServletRequest request) {
        LOGGER.debug("Processing login from: {}, hybrid: {}", service, hybrid);
        param.put("service", service);
        param.put("hybrid", hybrid);
        param.put("oauth_token", token);
        if(request.getSession().getAttribute("loginUrlQueryString") == null){
        	request.getSession().setAttribute("loginUrlQueryString", request.getQueryString());
        }
        if(StringConst.MOBILE.equalsIgnoreCase(device)){
        	return new ModelAndView("device", param);
        }
        else{
        	return new ModelAndView("login", param);
        }
    }

    @RequestMapping(value = "/accessDenied", method = RequestMethod.GET)
    public ModelAndView accessDenied() {
        return new ModelAndView("accessDenied");
    }

    @RequestMapping(value = "/authorize", method = RequestMethod.GET)
    public ModelAndView accessConfirm() {
        return new ModelAndView("authorize");
    }

}
